#include <algorithm>
#include <cstdio>

const int N = 25;
int n, m;
int e_cnt, heads[N], f[N][N];
bool vis[N];

struct Edge {
  int v, nxt, w;
} e[N << 1];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}

int dfs(int x, int fa) {
  vis[x] = true;
  for (int i = heads[x], v, flow = 0; i; i = e[i].nxt) {
    if ((v = e[i].v) != fa) {
      if (vis[v]) return e[i].w;
      if ((flow = dfs(v, x))) {
        return std::min(flow, e[i].w);
      }
    }
  }
  vis[x] = false;
  return 0;
}

void solve(int x) {
  for (int i = heads[x], v; i ; i = e[i].nxt) {
    
  }
}
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("T1.in", "r", stdin);
  freopen("T1.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1, a, b, c; i <= m; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    f[a][b] += c;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      // if (i == j) continue;
      if (f[i][j] > f[j][i]) {
        add(i, j, f[i][j] - f[j][i]);
      } else if (f[i][j] < f[j][i])
        add(j, i, f[j][i] - f[i][j]);
    }
  }
  while (true) {
    int flag = 0;
    for (int i = 1; i <= n; ++i) flag += dfs(i, i);
    if (!flag) break;
  }
  for (int i = 1;)
  return 0;
}